package com.buyouchina.service.privilege.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.buyouchina.bean.privilege.User;
import com.buyouchina.service.base.DaoSupport;
import com.buyouchina.service.privilege.UserService;
import com.buyouchina.utils.MD5;

@Service
public class UserServiceBean extends DaoSupport<User> implements UserService {

	
	String hql;
	
	public void setVisibleStatu(Integer[] ids, boolean statu) {
		if (ids != null && ids.length > 0) {
			StringBuffer jpql = new StringBuffer();
			for (int i = 0; i < ids.length; i++) {
				jpql.append('?').append((i + 2)).append(',');
			}
			jpql.deleteCharAt(jpql.length() - 1);
			Query query = em
					.createQuery("update User o set o.visible=?1 where o.id in("
							+ jpql.toString() + ")");
			query.setParameter(1, statu);
			for (int i = 0; i < ids.length; i++) {
				query.setParameter(i + 2, ids[i]);
			}
			query.executeUpdate();
		}
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public boolean exsit(String username) {
		long count = (Long) em.createQuery(
				"select count(o) from User o where o.phone=?1").setParameter(1,
				username).getSingleResult();
		return count > 0;
	}

	/**
	 * 修改密码
	 */
	public void updatePassword(String username, String newpassword) {
		em.createQuery("update User o set o.password=?1 where o.username=?2")
				.setParameter(1, MD5.MD5Encode(newpassword)).setParameter(2,
						username).executeUpdate();

	}

	public boolean checkUser(String username, String password) {
		long count;
		count = (Long) em
				.createQuery(
						"select count(o) from User o where o.phone=?1 and o.password=?2")
				.setParameter(1, username).setParameter(2,
						MD5.MD5Encode(password)).getSingleResult();
		System.out.print(MD5.MD5Encode(password));
		return count > 0;
	}

	public User findByPhone(String phone) {
		return (User) em.createQuery("select o from User o where o.phone=?1")
				.setParameter(1, phone).getSingleResult();
	}

	@Override
	public void delete(Serializable... entityids) {
		super.delete(entityids);
	}

	@Override
	public void save(Object entity) {
		User user = (User) entity;
		user.setPassword(MD5.MD5Encode(user.getPassword()));
		super.save(entity);
	}

	@SuppressWarnings("unchecked")
	public List<User> list(Integer outletsid) {
		return em.createQuery(
				"select u from User u inner join u.outlets o where o.id=?1")
				.setParameter(1, outletsid).getResultList();
	}

	/*
	@SuppressWarnings("unchecked")
	public List<User> listManager(Province p) {
		if (p != null) {
			String hql = "";
			if (p.getProvinceid() != null) {
				hql = "select u from User u inner join u.provinces p where p.id=?1 or p.cityid=?2";
				return em.createQuery(hql).setParameter(1, p.getId())
						.setParameter(2, p.getId().toString()).getResultList();
			} else if (p.getCityid() != null) {
				hql = "select u from User u inner join u.provinces p where p.id=?1";
				return em.createQuery(hql).setParameter(1, p.getId())
						.getResultList();
			} else {
				hql = "select u from User u inner join u.provinces p where p.id between ?1 and ?2";
				return em.createQuery(hql).setParameter(1, p.getId())
						.setParameter(2,
								ProvinceUtil.getLastIndexOfProvince(p.getId()))
						.getResultList();
			}
		} else {
			return getScrollData().getResultlist();
		}

	}

	public List<User> list(int firstindex, int maxresult, Province p) {
		if (p != null) {
			String hql = "";
			List<Object> params = new ArrayList<Object>();
			if (p.getProvinceid() != null) {
				hql = "select u from User u inner join u.provinces p where p.id=?1 or p.cityid=?2";
				params.add(p.getId());
				params.add(p.getId().toString());
				return list(hql, firstindex, maxresult, params.toArray());
			} else if (p.getCityid() != null) {
				hql = "select u from User u inner join u.provinces p where p.id=?1";
				params.add(p.getId());
				return list(hql, firstindex, maxresult, params.toArray());
			} else {
				hql = "select u from User u inner join u.provinces p where p.id between ?1 and ?2";
				params.add(p.getId());
				params.add(ProvinceUtil.getLastIndexOfProvince(p.getId()));
				return list(hql, firstindex, maxresult, params.toArray());
			}
		} else {
			return getScrollData(firstindex, maxresult).getResultlist();
		}

	}
*/
	public List<User> list(int firstindex, int maxresult, String queryString) {
		String hql = "from User u where u.realname like ?1 or u.phone=?2";
		List<Object> params = new ArrayList<Object>();
		params.add(queryString);
		params.add(queryString);
		return list(hql, firstindex, maxresult, params.toArray());
	}

	@SuppressWarnings("unchecked")
	public List<User> list(String queryString) {
		String hql = "from User u where u.realname like	?1 or u.phone=?2";
		return em.createQuery(hql).setParameter(1, "%"+queryString+"%").setParameter(2,
				queryString).getResultList();
	}

	public List<User> list(Integer firstindex, Integer maxresult, Integer pid,Integer epid) {
		hql="select u from User u inner join u.provinces p where p.id between ?1 and ?2";
		return list(hql,firstindex,maxresult,pid,epid);
	}

	@SuppressWarnings("unchecked")
	public List<User> listManager(Integer pid,Integer epid) {
		hql="select u from User u inner join u.provinces p where p.id between ?1 and ?2";
		return em.createQuery(hql).setParameter(1, pid).setParameter(2, epid).getResultList();
	}

}
